﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Input;
using Windows.UI.Xaml;

namespace MetroToolbox.Interactivity
{
    public class InvokeCommandAction : TriggerAction<DependencyObject>
    {
        public readonly static DependencyProperty CommandProperty = DependencyProperty.Register(
            "Command", 
            typeof(ICommand), 
            typeof(InvokeCommandAction), 
            new PropertyMetadata(null));

        public ICommand Command
        {
            get { return (ICommand)this.GetValue(CommandProperty); }
            set { this.SetValue(CommandProperty, value); }
        }

        public readonly static DependencyProperty CommandParameterProperty = DependencyProperty.Register(
            "CommandParameter",
            typeof(object),
            typeof(InvokeCommandAction),
            new PropertyMetadata(null));

        public object CommandParameter
        {
            get { return this.GetValue(CommandParameterProperty); }
            set { this.SetValue(CommandProperty, value); }
        }

        public string CommandName { get; set; }

        protected override void Invoke(object parameter)
        {
            if (IsEnabled)
            {
                var command = Command;

                if (command == null)
                {
                    // TODO: Get the command using the CommandName
                }

                if (command != null && command.CanExecute(CommandParameter))
                {
                    command.Execute(CommandParameter);
                }
            }
        }
    }
}
